﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace ProjectStudy
{
    public partial class Pepeater控件 : System.Web.UI.Page
    {
        private static DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sql = "select * from Class1";

                dt = DBAccess.ExecuteDataTable(sql);
                Repeater1.DataSource = dt;
                Repeater1.DataBind();

            }
            
        }

        protected void btnDeleteOneData(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DelItem")
            {
                int i = e.Item.ItemIndex;
                DataRow row = dt.Rows[i];
                string id = dt.Rows[i]["id"].ToString();
                dt.Rows.Remove(row);

                Repeater1.DataSource = dt;
                Repeater1.DataBind();

                string sql = "delete from Class1 where id=@id";
                SqlParameter ps = new SqlParameter("@id", id);
                DBAccess.ExecuteNonQuery(sql, ps);
            }

            if (e.CommandName == "EditItem")
            {
                Label5.Text = "点击了编辑按钮";
                Label5.ForeColor = RandomColor();
            }

            
        }

        public Color GetRandomColor()
        {
            Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
            // 对于C#的随机数，没什么好说的 
            System.Threading.Thread.Sleep(RandomNum_First.Next(50));
            Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);

            // 为了在白色背景上显示，尽量生成深色 
            int int_Red = RandomNum_First.Next(256);
            int int_Green = RandomNum_Sencond.Next(256);
            int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;

            return Color.FromArgb(int_Red, int_Green, int_Blue);
        }

        public Color RandomColor()
        { 
            Random ran = new Random();
            int r = ran.Next(0,255);
            int g = ran.Next(0, 255);
            int b = ran.Next(0, 255);
            if (r == 255 && g == 255 && b == 255)
            {
                RandomColor();
            }
            return Color.FromArgb(r, g, b);            
        }
    }
}